package com.example.demo.filter;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.util.RequestDto;
import com.example.demo.util.itfcheck.ItfCheckUtil;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class CheckParamFilterRequestWrapper extends HttpServletRequestWrapper{

	private JSONObject jsonObject;

	public CheckParamFilterRequestWrapper(HttpServletRequest request, RequestDto req) {
		super(request);
		String prikey = "sqsqwqeqweqweqweqe";
		String data = ItfCheckUtil.decryption(req, prikey);
		req.setData(data);
		jsonObject = (JSONObject) JSONObject.toJSON(req);    
	}
	
	@Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream bais = new ByteArrayInputStream(jsonObject.toString().getBytes());
        return new ServletInputStream() {

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }
			@Override
            public int read() throws IOException {

                return bais.read();

            }

            @Override
            public void close() throws IOException {
                bais.close();
            }
        };
    }
    

}
